package com.huifaapp.mall.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.DetailsofPaymenthongbaoAdapter;
import com.huifaapp.mall.bean.CouponListBean;
import com.huifaapp.mall.bean.GoodsListBean;
import com.huifaapp.mall.uitils.Contig;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/9.
 */

public class PosterWin extends PopupWindow implements View.OnClickListener {

//    private Button btnTakePhoto, btnSelect, btnCancel;
    private View mPopView;
    private OnItemClickListener mListener;
    private LinearLayout ll_poster_win;
    private LinearLayout ll_poster;
    ArrayList<CouponListBean> couponlistbean;
    LinearLayout pop_layout_poster;
    public PosterWin(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        init(context);
        setPopupWindow();
        ll_poster_win.setOnClickListener(this);
        pop_layout_poster.setOnClickListener(this);
    }

    /**
     * 初始化
     *
     * @param context
     */
    private void init(Context context) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = LayoutInflater.from(context);
        //绑定布局
        mPopView = inflater.inflate(R.layout.poster_win, null);
        ll_poster_win= (LinearLayout) mPopView.findViewById(R.id.ll_poster_win);
        pop_layout_poster= (LinearLayout) mPopView.findViewById(R.id.pop_layout_poster);
        ll_poster= (LinearLayout) mPopView.findViewById(R.id.ll_poster);
        createhongbaoItem(context,ll_poster_win, Contig.couponlistbean);

    }
    private void createhongbaoItem(Context context, ViewGroup viewGroup, ArrayList<CouponListBean> list){
        if (list == null){
            return;
        }
        viewGroup.removeAllViews();
        for (int i = 0 ;i<list.size();i++){
            CouponListBean attr =list.get(i);
            View convertView = LayoutInflater.from(context).inflate(R.layout.poster_win_hongbao, null);
            DetailsofPaymenthongbaoAdapter.ViewHolder viewHolder = new DetailsofPaymenthongbaoAdapter.ViewHolder(convertView);
            viewHolder.setdate(context,list.get(i));
            viewGroup.addView(convertView);
        }
    }
    public  void setList(ArrayList<CouponListBean> couponlistbean){
        this.couponlistbean=couponlistbean;
    }

    /**
     * 设置窗口的相关属性
     */
    @SuppressLint("InlinedApi")
    private void setPopupWindow() {
        this.setContentView(mPopView);// 设置View
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);// 设置弹出窗口的宽
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的高
        this.setFocusable(true);// 设置弹出窗口可
        this.setAnimationStyle(R.style.take_photo_anim);// 设置动画
        this.setBackgroundDrawable(new ColorDrawable(0x00000000));// 设置背景透明
        mPopView.setOnTouchListener(new View.OnTouchListener() {// 如果触摸位置在窗口外面则销毁

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                int height = mPopView.findViewById(R.id.pop_layout_poster).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
    }

    /**
     * 定义一个接口，公布出去 在Activity中操作按钮的单击事件
     */
    public interface OnItemClickListener {
        void setOnItemClick(View v);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (mListener != null) {
            mListener.setOnItemClick(v);
        }

    }

}

